package com.jvxb.manage.livable.controller;

import com.jvxb.manage.livable.entity.MysqlChange;
import com.jvxb.manage.livable.service.MysqlChangeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.jvxb.common.web.RespMsg;

/**
 * @author jvxb
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/livable/mysqlChange/v1")
public class MysqlChangeController {

    @Autowired
    private MysqlChangeService mysqlChangeService;

    @PostMapping("/add")
    public RespMsg add(@RequestBody MysqlChange mysqlChange) {
        mysqlChangeService.save(mysqlChange);
        return RespMsg.ok();
    }

    @PostMapping("/delete")
    public RespMsg delete(@RequestBody MysqlChange mysqlChange) {
        mysqlChangeService.removeById(mysqlChange);
        return RespMsg.ok();
    }

    @PostMapping("/update")
    public RespMsg update(@RequestBody MysqlChange mysqlChange) {
        mysqlChangeService.saveOrUpdate(mysqlChange);
        return RespMsg.ok();
    }

    @GetMapping("/getById")
    public RespMsg getById(Long id) {
        MysqlChange mysqlChange = mysqlChangeService.getById(id);
        return RespMsg.ok(mysqlChange);
    }

    @GetMapping("/list")
    public RespMsg list() {
        List<MysqlChange> list = mysqlChangeService.list(null);
        return RespMsg.ok(list);
    }

    @GetMapping("listPage")
    public RespMsg listPage(@RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<MysqlChange> mysqlChangeIPage = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        mysqlChangeService.page(mysqlChangeIPage, wrapper);
        return RespMsg.ok(mysqlChangeIPage);
    }
}
